package cc.rboot._core.helper;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

public class RequestHelper {

    private static final ThreadLocal<HttpServletRequest> THREAD_LOCAL_DATA = new ThreadLocal<HttpServletRequest>();

    public static void set(HttpServletRequest request) {
        THREAD_LOCAL_DATA.set(request);
    }

    public static HttpServletRequest get() {
        return THREAD_LOCAL_DATA.get();
    }

    public static void remove() {
        THREAD_LOCAL_DATA.remove();
    }

    public static void setAttribute(String name, Object object) {
        get().setAttribute(name, object);
    }

    public static Object getAttribute(String name) {
        HttpServletRequest httpServletRequest = get();
        if (httpServletRequest == null) {
            return null;
        }
        return httpServletRequest.getAttribute(name);
    }

    public static Object getParameter(String name) {
        HttpServletRequest httpServletRequest = get();
        if (httpServletRequest == null) {
            return null;
        }
        return httpServletRequest.getParameter(name);
    }

    public static Object getHeader(String name) {
        HttpServletRequest httpServletRequest = get();
        if (httpServletRequest == null) {
            return null;
        }
        return httpServletRequest.getHeader(name);
    }

    public static BufferedReader getReader() throws IOException {
        HttpServletRequest httpServletRequest = get();
        if (httpServletRequest == null) {
            return null;
        }
        return httpServletRequest.getReader();
    }

    public static <T> T getRequestBody(Class<T> clazz) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        try {
            HttpServletRequest httpServletRequest = get();
            if (httpServletRequest == null) {
                return null;
            }
            br = httpServletRequest.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return JSONObject.parseObject(sb.toString(), clazz);
    }

    public static String getRequestContent() {
        StringBuilder content = new StringBuilder();
        String inputLine;
        try (BufferedReader reader = get().getReader()) {
            while ((inputLine = reader.readLine()) != null) {
                content.append(inputLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    public static String content() throws IOException {
        StringBuilder content = new StringBuilder();
        String inputLine = "";
        try {
            while((inputLine = RequestHelper.get().getReader().readLine()) != null) {
                content.append(inputLine);
            }
            RequestHelper.get().getReader().close();
            return content.toString();
        } catch(Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            RequestHelper.get().getReader().close();
        }
    }
}
